﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIZMA._controls;
using BIZModel;
using BL.BIZControls;

namespace BIZMA._controls.Files
{
    public partial class Pick_Folder : System.Web.UI.UserControl
    {
        public Guid SelectedId { get { return ViewState["SelectedId"] != null ? (Guid)ViewState["SelectedId"] : Guid.Empty; } set { ViewState["SelectedId"] = value; } }
        public string Description { get { return lbl.Text; } set { lbl.Text = value; } }
        public string ToolTip { get { return lbl.ToolTip; } set { lbl.ToolTip = value; ddl.ToolTip = value; } }
        public bool Enabled { get { return btnOpen.Enabled; } set { btnOpen.Enabled = value; } }

        public delegate void SelectedIndexChanged(Guid selectedId);
        public event SelectedIndexChanged SelectedChanged;

        public bool InitLoaded { get { return ViewState["InitLoaded"] != null ? (bool)ViewState["InitLoaded"] : false; } set { ViewState["InitLoaded"] = value; } }
        public bool EditFunction { get { return btnOpen.Visible; } set { btnOpen.Visible = value; } }

        public string ReqValidationGroup { get { return reqVal.ValidationGroup; } set { reqVal.ValidationGroup = value; } }
        public bool ReqValEnabled { get { return reqVal.Enabled; } set { reqVal.Enabled = value; } }


        /// <summary>
        /// Sätt detta värde om mapparna som man väljer ska hämtas från en viss rootfolder
        /// Default är att man hämtar det utifrån SelectedId om det finns
        /// </summary>
        public Guid PickerRootFolder { get { return ViewState["PickerRootFolder"] != null ? (Guid)ViewState["PickerRootFolder"] : Guid.Empty; } set { ViewState["PickerRootFolder"] = value; } }



        #region Defaultddl
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack || !InitLoaded)
                Populate();


            //Här ska event som gäller så länge en kontroll är laddad registreras
            //OnInit sker innan så då är mContainer.ContainedControl alltid null
            if (mContainer.ContainedControl != null)
            {
                ((BIZListControl)mContainer.ContainedControl).ListSelect += new BIZListControl.SelectedChanged(Pick_ListSelect);
            }

        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (ToolTip == "")
                ToolTip = Description;

            if (reqVal.IsValid)
            {
                lbl.CssClass = lbl.CssClass.Replace(" error", "");
                ddl.CssClass = ddl.CssClass.Replace(" error", "");
            }
            else
            {
                lbl.CssClass += " error";
                ddl.CssClass += " error";
            }

            lbl.ID = ddl.ID + "_label";

            base.Render(writer);
        }



        #endregion

        #region Modal
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btnOpen.Visible = EditFunction && mContainer.CanAddPopupControl(BIZControl.FolderList);

            
        }


        protected void Open_Click(object sender, EventArgs e)
        {
            //När kontrollen laddats triggas detta event så man kan sätta olika inställningar på kontrollen
            //man behöver bara lyssna på det här eventet då man laddar in kontrollen inte på varje oninit
            mContainer.BIZControlLoaded += new ControlLoaded(mContainer_BIZControlLoaded);
            mContainer.AddDefaultListLoad(BIZControl.FolderList, SelectedId, List_Folders.LoadByCheckRootOnSected,true);
        }

        void mContainer_BIZControlLoaded(object sender, EventArgs e)
        {
            if (mContainer.ContainedControlExists)
            {

                ((List_Folders)mContainer.ContainedControl).MultipleSelect = false;

                //((List_Folders)mContainer.ContainedControl).SelectedItems.Clear();
                //((List_Folders)mContainer.ContainedControl).SelectedItems.Add(SelectedId);
                ((List_Folders)mContainer.ContainedControl).ApplySettings();
            }
        }

        void Pick_ListSelect(object sender, EventArgs e, bool multipleSelect, List<Guid> selectedItems)
        {
            Guid selectedId = Guid.Empty;

            if (selectedItems.Count > 0)
            {
                selectedId = selectedItems[0];
            }

            if (selectedId != SelectedId)
            {
                       SelectedId = selectedId;
                if (SelectedChanged != null)
                    SelectedChanged(SelectedId);

                Populate();
            }

     
        }

        #endregion

        public void Reload()
        {
            Populate();
        }

        private void Populate()
        {
            InitLoaded = true;
            BIZMAEntities ent = BaseBL.GetEnt();

            reqVal.InitialValue = Guid.Empty.ToString();

            ddl.Items.Clear();
            ddl.ToolTip = "Välj mapp";
            lbl.ToolTip = "Välj mapp";

            if (SelectedId == Guid.Empty)
            {
                ddl.Items.Add(new ListItem("--- Välj mapp---", Guid.Empty.ToString()));
            }
            else
            {
                BIZModel.Folder folder = ent.Folders.FirstOrDefault(q => q.FolderId == SelectedId);

                if (folder != null)
                {
                    ddl.ToolTip = string.Format("Mapp: {0} är vald",folder.FolderName);
                    lbl.ToolTip = string.Format("Mapp: {0} är vald", folder.FolderName);

                    ddl.Items.Add(new ListItem(folder.FolderName, folder.FolderId.ToString()));
                }
                else
                {
                    ddl.Items.Add(new ListItem("--- Välj mapp---", Guid.Empty.ToString()));
                }
            }

            ddl.SelectedValue = SelectedId.ToString();
        }





    }
}